/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 *
 * @author Unni Vemanchery Mana
 */
public class TestAnimation extends InputAdapter implements ApplicationListener {

    private final String spriteImage = "img1.png";
     private SpriteBatch batch;
     private Texture texture;
     private Sprite sprite;
     float x = 0;
     float y = 0;
     OrthographicCamera camera;
     SpriteObject [] spirteObjects ;
     private long time = 0;
     private boolean isAnimated = false;
     
    @Override
    public void create(){
        Texture.setEnforcePotImages(false);
        batch    = new SpriteBatch();
        texture  = new Texture(Gdx.files.internal(spriteImage));
        sprite   = new Sprite(texture);
        sprite.setSize(5, 5);
        camera   = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        initialize();
        time     = System.currentTimeMillis();
    }
    
    @Override
    public void resize(int i, int i1) {
    }

    @Override
    public void render() {
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   
      batch.begin();
      if(isAnimated){
      for(SpriteObject so : spirteObjects){
            so.y += so.yVelocity;
            so.x += so.xVelocity;
            so.yVelocity += 0.09;
            sprite.setPosition(so.x, so.y);
            sprite.draw(batch);
            if((System.currentTimeMillis() - time) >= so.timeToLive){
               so.y = -1;
               so.x = -1;
               isAnimated = false;
            }
        }
      }
       batch.end();
    }

    public void setAnimated(boolean isAnimated){
      this.isAnimated = isAnimated;
    }
    
    @Override
    public void pause() {
        
    }

    @Override
    public void resume() {
        
    }

    @Override
    public void dispose() {
        
    }
    
    private void initializationExplosionAnimation(){
    }
    
    private class SpriteObject {
       float x, y = 0;
       float scaleX,scaleY = 0;
       private float yVelocity = 0;
       private float xVelocity = 0;
       private long timeToLive = 400;
    }
    
    public void initialize(){
       spirteObjects = new SpriteObject[25];
       for(int i = 0; i < spirteObjects.length; i++){
           SpriteObject so = new SpriteObject();
           so.x = 140;
           so.y = 130;
           so.scaleX = (float) (0.2 + Math.random() * 0.4);
           so.scaleY = so.scaleX;
           so.yVelocity = (float)Math.random() * 4 - 5;
           so.xVelocity = (float)Math.random() * 6 - 3;
           so.timeToLive+=(float)Math.random() * 6;
           spirteObjects[i] = so;
       }
    }
}
